package com.axt.manager.action;

import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.axt.manager.bean.UserBean;
import com.axt.manager.model.User;
import com.axt.soa.action.BaseAction;
import com.axt.soa.msg.types.MsgParam;
import com.axt.util.TypeConvert;

public class UserAction extends BaseAction{
	private UserBean userBean = new UserBean();
	private List<User> users = null;
	private String userId;
	private String userRole;
	private String serverId;
	
	private String userName;
	private String personName;
	private String department;
	private String position;
	private String email;
	private String telephone;
	private String passwd;
	private String oldPasswd;
	
	@JSON(name="users")
	public List<User> getUsers(){
		return users;
	}
	private void setUsers(List<User> users){
		this.users = users;
	}
	@JSON(name="userid")
	public String getUserId(){
		return userId;
	}
	private void setUserId(String userId){
		this.userId = userId;
	}
	@JSON(name="userrole")
	public String getUserRole(){
		return userRole;
	}
	private void setUserRole(String userRole){
		this.userRole = userRole;
	}
	@JSON(name="errcode")
	public int getErrcode() {
		return errcode;
	}
	@JSON(name="errinfo")
	public String getErrinfo() {
		return errinfo;
	}
	
    public String getOldPasswd() {
		return oldPasswd;
	}
	public void setOldPasswd(String oldPasswd) {
		this.oldPasswd = oldPasswd;
	}
	public String getServerId() {
		return serverId;
	}
	public void setServerId(String serverId) {
		this.serverId = serverId;
	}
	public UserBean getUserBean() {
		return userBean;
	}
	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	
	//添加用户
	public String addUser() throws Exception {
		setUserId(TypeConvert.toString(userBean.addUser(userName, personName, 
				department, position, email, telephone, passwd)));
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	
	//获取用户列表
	public String queryUserList() throws Exception {
		
		setUsers(userBean.queryUserList());
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	
	// 获取用户角色
	public String queryUserRole() throws Exception {
		
		setUserRole(userBean.queryUserRole(userId, serverId));
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
    
	//删除用户
	public String deleteUser()throws Exception {
		
		userBean.deleteUser(userId);
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	
	//修改用户
	public String modifyUser()throws Exception {
		
		userBean.modifyUser(userId, userName, personName, 
				department, position, email, telephone, passwd);
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	
	//锁定用户
	public String lockUser()throws Exception {
		
		userBean.lockUser(userName);
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	//解锁用户
	public String unLockUser()throws Exception {
		
		userBean.unlockUser(userName);
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	//修改用户密码
	public String modifyPwd()throws Exception {
		
		userBean.modifyPwd(userName, passwd, oldPasswd);
		
    	setErrcode(userBean.getErrcode());
		setErrinfo(userBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
}
